﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace DefCore.Core
{
    public class DungeonPos : IEquatable<DungeonPos>
    {
        //Fields
        private Vector2 _worldPosition;

        /// <summary>
        /// The Overallposition in World Space, this is relative to (0,0) in the world
        /// </summary>
        public Vector2 WorldPosition
        {
            get { return _worldPosition; }
            set { _worldPosition = value; }
        }
        
        /// <summary>
        /// Gets the Position as a Finaite point on the map, representing the Tile
        /// </summary>
        public Point MapPosition
        {
            get { return new Point((int)(_worldPosition.X / Globals.TileSize.X), (int)(_worldPosition.Y / Globals.TileSize.Y)); }
        }

        /// <summary>
        /// Gets the Position relative to the top left of the screen. (NOTE: CAMERA)
        /// </summary>
        public Vector2 ScreenSpaceCoords
        {
            get { return WorldPosition - Objects.GameCamera.CameraTopLeft; }
        }
        
        /// <summary>
        /// Gets the Center of the Tile using ScreenSpace Coordiantes
        /// </summary>
        public Vector2 CenterTile
        {
            get
            {
                return new Vector2(
                    ScreenSpaceCoords.X + (Globals.TileSize.X / 2),
                    ScreenSpaceCoords.Y + (Globals.TileSize.X / 2));
            }
        }

        public int MapX { get { return MapPosition.X; } }
        public int MapY { get { return MapPosition.Y; } }
                
        //ctor
        private DungeonPos() { }
        public DungeonPos(Vector2 worldPosition)
        {
            this.WorldPosition = worldPosition;
        }
        public DungeonPos(Point mapPosition)
        {
            this.WorldPosition = new Vector2(mapPosition.X * Globals.TileSize.X, mapPosition.Y * Globals.TileSize.Y);
        }
        public DungeonPos(int x, int y)
            : this(new Point(x,y))
        {

        }

        //Methods
        public static DungeonPos operator +(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition + b.WorldPosition);
        }
        public static DungeonPos operator -(DungeonPos a, DungeonPos b)
        {
            return  new DungeonPos(a.WorldPosition - b.WorldPosition);
        }
        public static DungeonPos operator *(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition * b.WorldPosition);
        }
        public static DungeonPos operator /(DungeonPos a, DungeonPos b)
        {
            return new DungeonPos(a.WorldPosition / b.WorldPosition);
        }

        public static bool operator ==(DungeonPos a, Point b)
        {
            return (a.MapX == b.X && a.MapY == b.Y);
        }
        public static bool operator !=(DungeonPos a, Point b)
        {
            return (a.MapX != b.X && a.MapY != b.Y);
        }
        
        public static DungeonPos CreateFromMapPosition(Point mapPosition)
        {
            return new DungeonPos(mapPosition);
        }

        public bool Equals(DungeonPos other)
        {
            return other.WorldPosition == this.WorldPosition;
        }
    }
}
